<template>
	<view class="manage">
	
		<block v-if="addressInfo.length==0">
			<view class="all-address">
				<view v-for="(item,index) in 2" :key="index">

					<view class="border" @click="selectAddress(item)">
						<view class="flex-left-right" style="padding-top: 30rpx;">
							<text class="fs-18 bold">{{item.consignee}}联系人</text>
							<text>{{item.phone}}手机号</text>
						</view>
						<view style="padding:20rpx 0">
							地址
							{{item.province_name+item.city_name+item.area_name+item.address}}
						</view>
					</view>
					<view class="select">
						<view class="select-default flex-center" @click="setDefault(item,index)">
						<!-- 	<image :src="'/static/img/unchecked.svg'" mode="aspectFill" v-if="item.is_default==0">
							</image>
							<image :src="'/static/img/checked.svg'" mode="aspectFill" v-else></image>
							<text>设为默认地址</text> -->
						</view>
						<view class="operate">
							<text @click="edit(item)">编辑</text>
							<text @click="deleteAddress(item.id,index)">删除</text>
						</view>
					</view>

				</view>
			</view>
			<view style="height: 100rpx;"></view>
			<view class="fixed-style">
				<view class="c-button" @click="add">添加新地址</view>
			</view>
		</block>
		<block v-else>
			<view class="default-no">
				<image src="@/static/img/no_address.png" mode="aspectFit"></image>
				<text>暂时没有收货地址哦~</text>
				<view @click="add"> 添加地址</view>
			</view>

		</block>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				addressInfo: ''
			}
		},
		methods: {
			deleteAddress(id,index) {
				this.$showModal('确定要删除该地址吗', () => {
					let params = {
						address_id:id
					}
					this.$get(this.$api.orderDel, params, (res) => {
						let {
							data
						} = res;
						if (data.code == 1) {
							this.addressInfo.splice(index,1);
							this.$Toast('删除成功');
							
						}else{
							this.$Toast(data.msg);
						}
					})
				})
			},
			edit(item) {
				this.$u.throttle(()=>{
					uni.navigateTo({
						url: './edit?item='+JSON.stringify(item)
					})
				}, 500)
				
			},
			add() {
				uni.navigateTo({
					url: './add'
				})
			},
			getAddress() {
				let params = {
					token: uni.getStorageSync('userToken')
				}
				this.$get(this.$api.orderAddress, params, (res) => {
					let {
						data
					} = res;
					if (data.code == 1) {
						this.addressInfo = data.data;
					}
				})
			},
			setDefault(data, index) {
				for (let i in this.addressInfo) {
					this.addressInfo[i].is_default = 0;
				}
				this.addressInfo[index].is_default = 1;
				let params = {
					id: data.id
				}
				this.$get(this.$api.orderaddress_uppdate, params, (res) => {
					let {
						data
					} = res;
					if (data.code == 1) {
						this.$Toast('设为默认地址成功');
							uni.setStorageSync('address', [this.addressInfo[index]]);
					}
				})
			},
			selectAddress(item) {
				uni.setStorageSync('address', [item]);
				this.$back();
			}
		},
		onShow() {
			this.getAddress()
		}

	}
</script>

<style lang="scss" scoped>
	.manage {
		min-height: 100vh;
		display: flex;
		flex-direction: column;
		.all-address>view {
			background: white;
			padding: 0 30rpx;
			margin-bottom: 16rpx;

		}

		.select {
			height: 96rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
		}

		.select-default image {
			width: 32rpx;
			height: 32rpx;
			margin-right: 20rpx;
		}

		.operate text {
			width: 100rpx;
			height: 40rpx;
			line-height: 40rpx;
			margin-left: 31rpx;
			border: 1px solid #ccc;
			border-radius: 20rpx;
			display: inline-block;
			font-size: 24rpx;
			text-align: center;
			box-sizing: border-box;
		}

		.fixed {
			position: fixed;
			bottom: 0;
			background: white;
			padding: 10rpx 0;
			width: 100%;

			.custom {
				width: 630rpx;
			}
		}

		.default-no {
			// height: calc(100vh - 140rpx);
			background: white;
			display: flex;
			flex: 1;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			text{margin: -70rpx 0 29rpx;}
               image{
				   width:456rpx;
				   margin-top: -100rpx;
			   }
			view {
				background: #386FF0;
				color: white;
				height: 60rpx;
				width: 220rpx;
				text-align: center;
				line-height: 60rpx;
				border-radius: 30rpx;
			}
		}
	}
	
	.fixed-style {
		position: fixed;
		bottom: 0;
		padding: 10rpx 0;
		width: 100%;
		border-top: 1px solid #eee;
		background-color: white;
		z-index: 9;
		.c-button {
			display: flex;
			justify-content: center;
			align-items: center;
			color: white;
			border-radius: 39rpx;
			margin: 0 auto;
			background: $m-c;
			height: 78rpx;
			width: 690rpx;
			margin: 0 auto;
		}
	}
</style>
